﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace TasksDataXml.Data
{
    [XmlRoot("Categories")]
    public class CategoryContainer : BaseDataItem
    {
        private List<Category> _categoryList = new List<Category>(); 
        
        //public void Save(string root, out Exception ex)
        //{
        //    var serializer = new DataSerializer<CategoryContainer>();
        //    serializer.WriteFile()
        //}


        [XmlElement("Category")]
        public List<Category> CategoryList
        {
            get { return _categoryList; }
            set { _categoryList = value; }
        }



        [XmlIgnore]
        public CategoryContainer SubCategory { get; set; }

        [XmlIgnore]
        public override string Folder
        {
            get { return "Categories"; }
        }
        
        [XmlIgnore]
        public override string Filename
        {
            get { return "categories.xml"; }
        }

        public static string GetPath(string root)
        {
            var cat = new CategoryContainer();
            var path = cat.GetFolder(root);
            return Path.Combine(path, "categories.xml");
        }

        public Category FindCategory(List<Category> list, Guid guid, out List<Category> container)
        {
            container = list;
            if(list == null)
                return null;

            foreach (var category in list)
            {
                if (category.GuidId == guid)
                    return category;

                if(category.CategoryList != null)
                {
                    var cat = FindCategory(category.CategoryList, guid, out container);
                    if (cat != null)
                        return cat;

                    container = list;
                }
            }

            return null;
        }

        public void AddCategory(Category category)
        {
            category.NotifyParentMove = RemoveItem;
            CategoryList.Add(category);
        }

        public override void AttachCallbacks(IBaseDataItem parentCategory)
        {
            
        }

        public override IBaseDataItem RemoveItem(IBaseDataItem item)
        {
            var i = item as Category;
            if(i != null)
            {
                _categoryList.Remove(i);
            }

            return i;
        }

        public override void Dispose()
        {
            foreach (var category in CategoryList)
            {
                
            }
            base.Dispose();
            
        }
    }
}
